import Vue from 'vue'
import VueRouter from 'vue-router'
import { resError } from '../js/global'
import { getItem, getToken } from '@/utils/auth' // 验权
import store from '../store'
Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

/**
 * 静态路由
 */
export const constantRouterMap = [
    {
        path: '/login',
        meta: {
            icon: null,
            title: "首页",
        },
        hidden: true,
        sort: null,
        component: () => import('@/authorization'),
    },
    {
        path: '*',
        meta: {
            icon: null,
            title: "404",
        },
        hidden: true,
        sort: null,
        component: () => import('@/404'),
    },
    {
        path: '/index',
        name: "index",
        component: () => import('@/pages'),
        meta: {
            icon: "el-icon-s-home",
            title: "首页",
        },
        hidden: false,
        sort: null,
        children: [
            {
                path: 'dashboard',
                name: "dashboard",
                meta: {
                    icon: null,
                    title: "桌面",
                },
                hidden: null,
                sort: null,
                component: () => import('@/pages/MainContent/Home/Dashboard'),
            },
        ]
    },
]
/**
 * 动态路由
 */
export const asyncRouterMap = [
    {
        path: '/user',
        name: "user",
        meta: {
            icon: null,
            title: null,
        },
        hidden: null,
        sort: null,
        component: () => import('@/pages'),
        children: [
            {
                path: 'userList',
                name: "userList",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/pages/MainContent/User/UserList'),
            },
        ]
    },
    {
        path: '/doctor',
        name: "doctor",
        meta: {
            icon: null,
            title: null,
        },
        hidden: null,
        sort: null,
        component: () => import('@/pages'),
        children: [
            {
                path: 'doctorList',
                name: "doctorList",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/pages/MainContent/Doctor/DoctorList'),
            },
            {
                path: 'doctorInfo',
                name: "doctorInfo",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/pages/MainContent/Doctor/DoctorInfo'),
            },
            {
                path: 'message',
                name: "doctorMessage",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/pages/MainContent/Doctor/DoctorMessage'),
            },
        ]
    },
    {
        path: '/adminMenu',
        name: "adminMenu",
        meta: {
            icon: null,
            title: null,
        },
        hidden: null,
        sort: null,
        component: () => import('@/pages'),
        children: [
            {
                path: 'adminMenuList',
                name: "adminMenuList",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/pages/MainContent/AdminMenu/AdmnMenuList'),
            },
            {
                path: 'adminMenuAdd',
                name: "adminMenuAdd",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/pages/MainContent/AdminMenu/AdminMenuAdd'),
            },
        ]
    },
    {
        path: '/permission',
        name: "permission",
        meta: {
            icon: null,
            title: null,
        },
        hidden: null,
        sort: null,
        component: () => import('@/pages'),
        children: [
            {
                path: 'userPermission',
                name: "userPermission",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/pages/MainContent/Permission/UserPermission'),
            },
            {
                path: 'rolePermission',
                name: "rolePermission",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/pages/MainContent/Permission/RolePermission'),
            },
            {
                path: 'loginLog',
                name: "loginLog",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/pages/MainContent/Permission/LoginLog'),
            },
            {
                path: 'permissionAdd',
                name: "permissionAdd",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/pages/MainContent/Permission/PermissionAdd'),
            },
        ]
    },
    {
        path: '/order',
        name: "order",
        meta: {
            icon: null,
            title: null,
        },
        hidden: null,
        sort: null,
        component: () => import('@/pages'),
        children: [
            {
                path: 'orderList',
                name: "orderList",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/pages/MainContent/Order/OrderList'),
            },
            {
                path: 'orderTask',
                name: "orderTask",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/pages/MainContent/Order/OderTask'),
            },
            {
                path: 'orderDoctor',
                name: "orderDoctor",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/pages/MainContent/Order/OrderDoctor'),
            },
        ]
    },
    {
        path: '/hospital',
        name: "hospital",
        meta: {
            icon: null,
            title: null,
        },
        hidden: null,
        sort: null,
        component: () => import('@/pages'),
        children: [
            {
                path: 'information',
                name: "hospitalInformation",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/pages/MainContent/Hospital/Information'),
            },
            {
                path: 'hospitalList',
                name: "hospitalList",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/pages/MainContent/Hospital/HospitalList'),
            },
        ]
    },
]


const router = new VueRouter({
    mode: 'history',
    routes: constantRouterMap,
    // 控制路由跳转滚动条的位置为最顶部
    scrollBehavior(to, from, savedPosition) {
        // 始终滚动到顶部，y代表距离顶部的距离
        return { y: 0 }
    },
})

const whiteList = ['/login'] // 不重定向白名单

router.beforeEach((to, from, next) => {
    if (getItem("token")) {
        if (to.path === '/login') {
            next({ path: '/index' })
        } else {
            if (store.state.user.role.length === 0) {
                // 拉取用户信息
                getUserInfo(next)
            } else {
                next()
            }
        }
    } else {
        if (whiteList.indexOf(to.path) !== -1) {
            next()
        } else {
            next('/login')
        }
    }
    next()
})

async function getUserInfo(next) {
    // 获取用户身份
    const identify = getItem("identify")
    let res = await store.dispatch('getUserInfo', identify)
    if (res.success) {
        let menus = null, username = null
        if (identify == 1) {
            menus = res.data.adminMenuList;
            username = res.data.userDto.username;
        }
        else if (identify == 2) {
            menus = res.data.adminMenuList;
            username = res.data.doctorDto.username;
        }
        else {
            menus = res.data.adminMenuList;
            username = res.data.adminDto.username;
        }
        // 生成可访问的路由表
        store.dispatch('GenerateRoutes', { menus, username }).then(() => {
            // 动态添加可访问路由表
            router.addRoutes(store.state.menu.addRouters);
            next()
        })
    }
    else {
        // 退出登录
        // let res = await store.dispatch('logout')
        // if (res.success) {
        //     Message.error(err || 'Verification failed, please login again')
        //     next({ path: '/' })
        // }
        resError(res)
    }

}


export default router